package cn.wsn2018.tools.ctrl;

import cn.wsn2018.tools.model.MySessionContext;
import cn.wsn2018.tools.model.VO.ConfigInfoView;
import cn.wsn2018.tools.service.ProtocolConfigService;
import cn.wsn2018.tools.util.ServletUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;
import java.util.List;

/**
 * Created by NZX on 2018/12/20.
 */
@RestController
@RequestMapping("/protocol")
public class ProtocolConfigController {

    @Autowired
    private ProtocolConfigService protocolConfigService;
    @CrossOrigin
    @RequestMapping(value = "/protocolConfigs/id",method = RequestMethod.GET)
    /**
     * 查询用户某个协议详细配置信息
     */
    public void findProtocolConfig(@RequestParam("id")String id, HttpServletRequest request, HttpServletResponse response)throws Exception{
        ServletUtil.getInstance().requestOperating(request);
        String respJson= protocolConfigService.dealProtocolConfigQuery(id);
        ServletUtil.getInstance().responseOperating(response, respJson);
    }



    @CrossOrigin
    @RequestMapping(value = "/protocolConfigs/designId",method = RequestMethod.GET)
    /**
     * 查询用户某个协议详细配置信息
     */
    public void findProtocolConfigByDesignId(@RequestParam("designId")String designId, HttpServletRequest request, HttpServletResponse response)throws Exception{
        ServletUtil.getInstance().requestOperating(request);
        String respJson= protocolConfigService.dealProtocolConfigQueryByDesignId(designId);
        ServletUtil.getInstance().responseOperating(response, respJson);
    }
    @CrossOrigin
    @RequestMapping(value = "/protocolConfigs/username",method = RequestMethod.GET)
    /**
     * 查询某个用户所有配置
     */
    public void listProtocolConfigs(@RequestParam("username")String username,@RequestParam("pageNum") String pageNum,
                                    @RequestParam("pageSize")String pageSize, HttpServletRequest request, HttpServletResponse response)throws Exception{
        ServletUtil.getInstance().requestOperating(request);
        String respJson= protocolConfigService.dealProtocolConfigsQuery(username,pageNum,pageSize);
        ServletUtil.getInstance().responseOperating(response, respJson);
    }
    @CrossOrigin
    @RequestMapping(value = "/protocolConfigs",method = RequestMethod.POST)
    /**
     * 保存用户协议配置信息
     */
    public void saveProtocolConfig(@RequestParam("token") String token, @RequestBody ConfigInfoView configInfoView, HttpServletRequest request, HttpServletResponse response)throws Exception{

        ServletUtil.getInstance().requestOperating(request);
        MySessionContext myc= MySessionContext.getInstance();
        HttpSession session = myc.getSession(token);
        String myUsername = session.getAttribute("username").toString();
        String respJson= protocolConfigService.dealProtocolConfigSave(configInfoView,myUsername);
        ServletUtil.getInstance().responseOperating(response, respJson);
    }
    @CrossOrigin
    @RequestMapping(value = "/protocolConfigs/id",method = RequestMethod.PUT)
    /**
     * 修改协议配置信息
     */
    public void updateProtocolConfig(@RequestParam("token") String token, @RequestBody ConfigInfoView configInfoView, HttpServletRequest request, HttpServletResponse response)throws Exception{
        ServletUtil.getInstance().requestOperating(request);
        MySessionContext myc= MySessionContext.getInstance();
        HttpSession session = myc.getSession(token);
        String myUsername = session.getAttribute("username").toString();
        String respJson = protocolConfigService.dealProtocolConfigUpdate(configInfoView,myUsername);
        ServletUtil.getInstance().responseOperating(response, respJson);
    }
    @CrossOrigin
    @RequestMapping(value = "/protocolConfigs/id",method = RequestMethod.DELETE)
    /**
     * 删除用户的某一协议配置
     */
    public void deleteProtocolConfig(@RequestParam("id")String id, @RequestParam("token") String token, HttpServletRequest request,HttpServletResponse response) throws Exception{
        ServletUtil.getInstance().requestOperating(request);
        MySessionContext myc= MySessionContext.getInstance();
        HttpSession session = myc.getSession(token);
        String myUsername = session.getAttribute("username").toString();
        String respJson = protocolConfigService.dealProtocolConfigDelete(id,myUsername);
        ServletUtil.getInstance().responseOperating(response, respJson);

    }
    @CrossOrigin
    @RequestMapping(value = "/protocolConfigs",method = RequestMethod.DELETE)
    /**
     * 删除用户的多条协议配置
     */
    public void deleteProtocolConfigs(@RequestParam("idList")List<String> idList,@RequestParam("token") String token,  HttpServletRequest request, HttpServletResponse response) throws Exception{
        ServletUtil.getInstance().requestOperating(request);
        MySessionContext myc= MySessionContext.getInstance();
        HttpSession session = myc.getSession(token);
        String myUsername = session.getAttribute("username").toString();
        String respJson = protocolConfigService.dealProtocolConfigsDelete(idList,myUsername);
        ServletUtil.getInstance().responseOperating(response, respJson);

    }
}
